﻿using System;
using HslCommunication.Profinet.Fuji;

namespace HslCommunication.Core.Address
{
	/// <summary>
	/// FujiSPB的地址信息，可以携带数据类型，起始地址操作
	/// </summary>
	// Token: 0x02000167 RID: 359
	public class FujiSPBAddress : DeviceAddressBase
	{
		/// <summary>
		/// 数据的类型代码
		/// </summary>
		// Token: 0x17000617 RID: 1559
		// (get) Token: 0x06001DDE RID: 7646 RVA: 0x0008DEBF File Offset: 0x0008C0BF
		// (set) Token: 0x06001DDF RID: 7647 RVA: 0x0008DEC7 File Offset: 0x0008C0C7
		public string TypeCode { get; set; }

		/// <summary>
		/// 当是位地址的时候，用于标记的信息
		/// </summary>
		// Token: 0x17000618 RID: 1560
		// (get) Token: 0x06001DE0 RID: 7648 RVA: 0x0008DED0 File Offset: 0x0008C0D0
		// (set) Token: 0x06001DE1 RID: 7649 RVA: 0x0008DED8 File Offset: 0x0008C0D8
		public int BitIndex { get; set; }

		/// <summary>
		/// 获取读写字数据的时候的地址信息内容
		/// </summary>
		/// <returns>报文信息</returns>
		// Token: 0x06001DE2 RID: 7650 RVA: 0x0008DEE4 File Offset: 0x0008C0E4
		public string GetWordAddress()
		{
			return this.TypeCode + FujiSPBOverTcp.AnalysisIntegerAddress((int)base.Address);
		}

		/// <summary>
		/// 获取命令，写入字地址的某一位的命令内容
		/// </summary>
		/// <returns>报文信息</returns>
		// Token: 0x06001DE3 RID: 7651 RVA: 0x0008DF0C File Offset: 0x0008C10C
		public string GetWriteBoolAddress()
		{
			int num = (int)(base.Address * 2);
			int num2 = this.BitIndex;
			bool flag = num2 >= 8;
			if (flag)
			{
				num++;
				num2 -= 8;
			}
			return string.Format("{0}{1}{2:X2}", this.TypeCode, FujiSPBOverTcp.AnalysisIntegerAddress(num), num2);
		}

		/// <summary>
		/// 按照位为单位获取相关的索引信息
		/// </summary>
		/// <returns>位数据信息</returns>
		// Token: 0x06001DE4 RID: 7652 RVA: 0x0008DF60 File Offset: 0x0008C160
		public int GetBitIndex()
		{
			return (int)(base.Address * 16) + this.BitIndex;
		}

		/// <summary>
		/// 从实际的Fuji的地址里面解析出地址对象<br />
		/// Resolve the address object from the actual Fuji address
		/// </summary>
		/// <param name="address">富士的地址数据信息</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06001DE5 RID: 7653 RVA: 0x0008DF84 File Offset: 0x0008C184
		public static OperateResult<FujiSPBAddress> ParseFrom(string address)
		{
			return FujiSPBAddress.ParseFrom(address, 0);
		}

		/// <summary>
		/// 从实际的Fuji的地址里面解析出地址对象<br />
		/// Resolve the address object from the actual Fuji address
		/// </summary>
		/// <param name="address">富士的地址数据信息</param>
		/// <param name="length">读取的数据长度</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06001DE6 RID: 7654 RVA: 0x0008DFA0 File Offset: 0x0008C1A0
		public static OperateResult<FujiSPBAddress> ParseFrom(string address, ushort length)
		{
			FujiSPBAddress fujiSPBAddress = new FujiSPBAddress();
			try
			{
				fujiSPBAddress.BitIndex = HslHelper.GetBitIndexInformation(ref address);
				char c = address[0];
				char c2 = c;
				if (c2 <= 'Y')
				{
					if (c2 <= 'D')
					{
						if (c2 == 'C')
						{
							goto IL_216;
						}
						if (c2 != 'D')
						{
							goto IL_320;
						}
						goto IL_2B4;
					}
					else
					{
						if (c2 == 'L')
						{
							goto IL_153;
						}
						if (c2 == 'M')
						{
							goto IL_12C;
						}
						switch (c2)
						{
						case 'R':
							goto IL_2D8;
						case 'S':
						case 'U':
						case 'V':
							goto IL_320;
						case 'T':
							goto IL_17A;
						case 'W':
							goto IL_2FC;
						case 'X':
							break;
						case 'Y':
							goto IL_105;
						default:
							goto IL_320;
						}
					}
				}
				else if (c2 <= 'd')
				{
					if (c2 == 'c')
					{
						goto IL_216;
					}
					if (c2 != 'd')
					{
						goto IL_320;
					}
					goto IL_2B4;
				}
				else
				{
					if (c2 == 'l')
					{
						goto IL_153;
					}
					if (c2 == 'm')
					{
						goto IL_12C;
					}
					switch (c2)
					{
					case 'r':
						goto IL_2D8;
					case 's':
					case 'u':
					case 'v':
						goto IL_320;
					case 't':
						goto IL_17A;
					case 'w':
						goto IL_2FC;
					case 'x':
						break;
					case 'y':
						goto IL_105;
					default:
						goto IL_320;
					}
				}
				fujiSPBAddress.TypeCode = "01";
				fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(1), 10);
				goto IL_330;
				IL_105:
				fujiSPBAddress.TypeCode = "00";
				fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(1), 10);
				goto IL_330;
				IL_12C:
				fujiSPBAddress.TypeCode = "02";
				fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(1), 10);
				goto IL_330;
				IL_153:
				fujiSPBAddress.TypeCode = "03";
				fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(1), 10);
				goto IL_330;
				IL_17A:
				bool flag = address[1] == 'N' || address[1] == 'n';
				if (flag)
				{
					fujiSPBAddress.TypeCode = "0A";
					fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(2), 10);
					goto IL_330;
				}
				bool flag2 = address[1] == 'C' || address[1] == 'c';
				if (flag2)
				{
					fujiSPBAddress.TypeCode = "04";
					fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(2), 10);
					goto IL_330;
				}
				throw new Exception(StringResources.Language.NotSupportedDataType);
				IL_216:
				bool flag3 = address[1] == 'N' || address[1] == 'n';
				if (flag3)
				{
					fujiSPBAddress.TypeCode = "0B";
					fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(2), 10);
					goto IL_330;
				}
				bool flag4 = address[1] == 'C' || address[1] == 'c';
				if (flag4)
				{
					fujiSPBAddress.TypeCode = "05";
					fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(2), 10);
					goto IL_330;
				}
				throw new Exception(StringResources.Language.NotSupportedDataType);
				IL_2B4:
				fujiSPBAddress.TypeCode = "0C";
				fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(1), 10);
				goto IL_330;
				IL_2D8:
				fujiSPBAddress.TypeCode = "0D";
				fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(1), 10);
				goto IL_330;
				IL_2FC:
				fujiSPBAddress.TypeCode = "0E";
				fujiSPBAddress.Address = Convert.ToUInt16(address.Substring(1), 10);
				goto IL_330;
				IL_320:
				throw new Exception(StringResources.Language.NotSupportedDataType);
				IL_330:;
			}
			catch (Exception ex)
			{
				return new OperateResult<FujiSPBAddress>(ex.Message);
			}
			return OperateResult.CreateSuccessResult<FujiSPBAddress>(fujiSPBAddress);
		}
	}
}
